package com.rambo.baselib.mvp;

import com.rambo.baselib.util.UiExecutor;
import ohos.aafwk.ability.Ability;

import java.lang.ref.WeakReference;

public class BasePresenter<V extends IView> implements IView {
    private WeakReference<V> iViewRef;


    public BasePresenter() {

    }
    public void attach(V iView){
        this.iViewRef = new WeakReference<V>(iView);
    }
    public boolean isAttach() {
        return iViewRef != null;
    }

    public void detach() {
        iViewRef.clear();
    }

    protected V getView(){
        return iViewRef.get();
    }


    @Override
    public void showLoading() {
        showLoading("加载中...", false);
    }

    @Override
    public void showLoading(String msg, boolean isDialogStyle) {
        if (!isAttach()) {
            return;
        }
        UiExecutor.getInstance().runOnMainThread(new Runnable() {
            @Override
            public void run() {
                if (!isAttach()) {
                    return;
                }
                iViewRef.get().showLoading(msg, isDialogStyle);
            }
        });

    }

    @Override
    public void hideLoading() {
        if (!isAttach()) {
            return;
        }
        UiExecutor.getInstance().runOnMainThread(new Runnable() {
            @Override
            public void run() {
                if (!isAttach()) {
                    return;
                }
                iViewRef.get().hideLoading();
            }
        });
    }
}
